#include <stdexcept>
#include "include/Configuration.h"
#include "include/Singleton.h"
#include "include/Server.h"
#include "include/SessionManager.h"
#include "include/Manager.h"

int main(int argc, char *argv[])
{
	try
	{
		// default config file
		std::string config_file_name = "../conf/config.txt";
		
		if (argc != 2 && argc != 1)
		{
			std::cerr << "Usage: Gameserver [config_file]" << std::endl;
			return 1;
		}

		if(argc == 2)
		{
			config_file_name = argv[1];
		}

		Singleton<Configuration>::instance().Init(config_file_name);
		if(Singleton<Configuration>::instance().ReadConfig())
		{
			return 1;
		}

		Singleton<SessionManager>::instance();
		Singleton<Manager>::instance().Init();
		if(Singleton<DelayBufferPool>::instance().Init(Singleton<Configuration>::instance().delay_buffer_num))
		{
			return 1;
		}

		if (Singleton<Configuration>::instance().thread_count <= 0)
		{
			std::cerr << "thread_count <= 0" << std::endl;
			return 1;           
		}       

		Server s;
		s.Run();
	}
	catch (std::exception& e)
	{
		std::cerr << "Exception: " << e.what() << std::endl;
		return 1;
	}

	return 0;
}
